package com.ssyc.gsk_teacher.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.Constants;
import com.ssyc.common.bean.ResultInfo;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.utils.AlertDialogUtil;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.bean.RedPackageInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 验证家长红包列表适配器
 * Created by Kevin on 2018/6/26
 */

public class RedPackageRvAdapter extends BaseQuickAdapter<RedPackageInfo.RedPacketBean, BaseViewHolder> {
    private Context context;


    public RedPackageRvAdapter(Context context, int layoutResId, @Nullable List<RedPackageInfo.RedPacketBean> data) {
        super(layoutResId, data);
        this.context = context;
    }


    @Override
    protected void convert(BaseViewHolder helper, RedPackageInfo.RedPacketBean item) {
        RelativeLayout rlMoney = helper.getView(R.id.rl_money);
        LinearLayout ll_out = helper.getView(R.id.ll_out);

        //红包金额
        TextView tvMoney = helper.getView(R.id.tv_money);
        tvMoney.setText("￥" + item.getMoney());

        //红包标题
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.getSchool() + "");

        //红包编号
        TextView tvNum = helper.getView(R.id.tv_no);
        tvNum.setText("红包编号:" + item.getId());

        //领取时间
        TextView tvTime = helper.getView(R.id.tv_time);
        tvTime.setText(item.getExpirationDay() + "到期");

        //领取人手机号
        TextView tvTel = helper.getView(R.id.tv_tel);
        tvTel.setText("领取人手机号:" + item.getCustomer());

        //状态
        ImageView lvFlag = helper.getView(R.id.lv_flag);
        if ("2".equals(item.getStatus())) {
            //表示用过
            lvFlag.setVisibility(View.VISIBLE);
            lvFlag.setImageResource(R.drawable.teacher_flag_used);
            rlMoney.setBackgroundResource(R.drawable.teacher_red_hasused);

        } else if ("1".equals(item.getStatus())) {
            //表示红包发出但未被抢
            lvFlag.setVisibility(View.GONE);
            rlMoney.setBackgroundResource(R.drawable.teacher_red_nouse);

        } else if ("3".equals(item.getStatus())) {
            //表示已失效
            lvFlag.setVisibility(View.VISIBLE);
            lvFlag.setImageResource(R.drawable.teacher_flag_time_out);
            rlMoney.setBackgroundResource(R.drawable.teacher_red_hasused);
        }


        ll_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(item.getStatus())) {
                    //使用
                    AlertDialogUtil.showAlertDialog(context, "确认使用编号为" + item.getId() + "的红包吗？"
                            , new AlertDialogUtil.onClickListener() {
                                @Override
                                public void onClickOk() {
                                    http(item);
                                }
                            });
                }
            }
        });
    }

    /**
     * 请求服务器数据的方法
     *
     */
    private void http(RedPackageInfo.RedPacketBean item) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "5");
        map.put("c", item.getCustomer());
        map.put("role", "1");
        map.put("redId", item.getId()+"");
        map.put("platform", "8");
        HttpUtils.post(HttpAdress.TEACHERREDLIST, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast(BaseApplication.ERROR,false);
            }

            @Override
            public void onResponse(String response, int id) {

                if (!TextUtils.isEmpty(response)) {
                    ResultInfo info = null;

                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }

                    Log.i("test", "使用红包:"+response);

                    if (null != info && "1".equals(info.getState())) {
                        //返回成功
                        UiUtils.Toast("使用成功",false);
                        item.setStatus("2");
                        notifyDataSetChanged();

                    } else if("-1".equals(info.getState())){
                        UiUtils.Toast("使用失败",false);

                    }else{
                        Log.i("test", Constants.ERRORSTATE + info.getState());
                    }
                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }
}
